#!/usr/bin/env python3
"""
Agent准确率测试类
测试分析准确率，并验证自主学习效果
"""

import json
import os
import sys
from pathlib import Path
from datetime import datetime

# 添加项目根目录到Python路径
project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root))

from app.services.core_service import CoreService

class AccuracyTester:
    """准确率测试类"""
    
    def __init__(self):
        self.core_service = None
        self.test_results = []
        self.accuracy_stats = {}
    
    def setup(self):
        """设置测试环境"""
        # 设置环境变量
        os.environ["OPENAI_API_KEY"] = "sk-gX6NNINB54pDBGRka36jg33fR1fnSOwfu21uC3Wnjmiwv3KB"
        os.environ["OPENAI_API_BASE"] = "https://api.chatanywhere.tech/v1"
        
        # 初始化核心服务
        self.core_service = CoreService()
        self.core_service.initialize()
    
    def load_test_data(self):
        """加载测试数据"""
        # 加载新闻数据
        with open('data/processed_news.json', 'r', encoding='utf-8') as f:
            self.news_data = json.load(f)
        
        # 加载实际结果
        with open('data/actual_results.json', 'r', encoding='utf-8') as f:
            self.actual_results = json.load(f)
    
    def test_accuracy(self, round_num=None):
        """测试准确率"""
        test_results = []
        correct_predictions = 0
        total_predictions = 0
        
        for i, (news_data, actual_result) in enumerate(zip(self.news_data, self.actual_results), 1):
            # 分析新闻
            result = self.core_service.analyze_news(news_data)
            
            if result['status'] == 'success' and 'price_analysis' in result:
                prediction = result['price_analysis']['price_prediction']
                actual = actual_result['actual_result']['direction']
                
                # 判断预测是否正确
                is_correct = prediction == actual
                if is_correct:
                    correct_predictions += 1
                total_predictions += 1
                
                # 记录关键测试结果
                test_result = {
                    "id": news_data['news_id'],
                    "prediction": prediction,
                    "actual": actual,
                    "confidence": result['price_analysis']['confidence']
                }
                test_results.append(test_result)
        
        # 计算准确率
        accuracy = correct_predictions / total_predictions if total_predictions > 0 else 0
        
        # 创建轮次结果
        round_result = {
            "round": round_num,
            "accuracy": accuracy,
            "timestamp": datetime.now().isoformat(),
            "results": test_results
        }
        
        return round_result
    
    def test_learning_effect(self, rounds=2):
        """测试学习效果 - 批量执行和可控制轮次"""
        # 创建统一结果文件
        os.makedirs('result', exist_ok=True)
        result_file = "result/accuracy_comparison.json"
        
        # 如果文件存在，读取现有结果
        all_rounds_results = []
        if os.path.exists(result_file):
            with open(result_file, 'r', encoding='utf-8') as f:
                all_rounds_results = json.load(f)
        
        for round_num in range(rounds):
            # 测试当前轮次
            round_result = self.test_accuracy(round_num + 1)
            all_rounds_results.append(round_result)
            
            # 如果不是最后一轮，进行学习
            if round_num < rounds - 1:
                for news_data, actual_result in zip(self.news_data, self.actual_results):
                    result = self.core_service.analyze_news(news_data)
                    if result['status'] == 'success' and 'price_analysis' in result:
                        self.core_service.learn_from_result(
                            news_data, 
                            result['price_analysis'], 
                            actual_result['actual_result']
                        )
        
        # 保存所有轮次结果到统一文件
        with open(result_file, 'w', encoding='utf-8') as f:
            json.dump(all_rounds_results, f, ensure_ascii=False, indent=2)
        
        return all_rounds_results


def main():
    """主函数"""
    # 创建测试器
    tester = AccuracyTester()
    
    # 设置测试环境
    tester.setup()
    
    # 加载测试数据
    tester.load_test_data()
    
    # 选择测试模式
    if len(sys.argv) > 1 and sys.argv[1] == "learn":
        # 测试学习效果，支持轮次控制
        rounds = int(sys.argv[2]) if len(sys.argv) > 2 else 2
        tester.test_learning_effect(rounds)
    else:
        # 单次准确率测试
        result = tester.test_accuracy(1)
        os.makedirs('result', exist_ok=True)
        with open('result/accuracy_comparison.json', 'w', encoding='utf-8') as f:
            json.dump([result], f, ensure_ascii=False, indent=2)

if __name__ == "__main__":
    main()
